package com.ruoyi.system.controller;

import com.alibaba.fastjson.JSON;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.WebUser;
import com.ruoyi.system.domain.WebUserShare;
import com.ruoyi.system.service.IWebUserService;
import com.ruoyi.system.service.IWebUserShareService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 客户分享Controller
 * 
 * @author wsliupan@126.com
 * @date 2021-07-02
 */
@Controller
@RequestMapping("/system/userShare")
public class WebUserShareController extends BaseController
{
    private String prefix = "system/userShare";

    @Autowired
    private IWebUserShareService webUserShareService;
    @Autowired
    private IWebUserService iWebUserService;

    @RequiresPermissions("system:userShare:view")
    @GetMapping()
    public String userShare()
    {
        return prefix + "/userShare";
    }

    /**
     * 查询客户分享列表
     */
    @RequiresPermissions("system:userShare:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(WebUserShare webUserShare)
    {
        startPage();
        List<WebUserShare> list = webUserShareService.selectWebUserShareList(webUserShare);
        return getDataTable(list);
    }

    /**
     * 导出客户分享列表
     */
    @RequiresPermissions("system:userShare:export")
    @Log(title = "客户分享", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(WebUserShare webUserShare)
    {
        List<WebUserShare> list = webUserShareService.selectWebUserShareList(webUserShare);
        ExcelUtil<WebUserShare> util = new ExcelUtil<WebUserShare>(WebUserShare.class);
        return util.exportExcel(list, "客户分享数据");
    }

    /**
     * 新增客户分享
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存客户分享
     */
    @RequiresPermissions("system:userShare:add")
    @Log(title = "客户分享", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(WebUserShare webUserShare)
    {
        return toAjax(webUserShareService.insertWebUserShare(webUserShare));
    }

    /**
     * 修改客户分享
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        WebUserShare webUserShare = null;
        if(id == null || id ==  -1){
            webUserShare = new WebUserShare();
        }else{
            webUserShare = webUserShareService.selectWebUserShareById(id);
            webUserShare.setRealname((webUserShare.getNickname()==null?"":webUserShare.getNickname())+"-"+(webUserShare.getRealname()==null?"":webUserShare.getRealname()));
        }
        mmap.put("webUserShare", webUserShare);
        WebUser user = new WebUser();
        List<WebUser> users = iWebUserService.selectWebUserList(user);
        mmap.put("users", JSON.toJSONString(users));
        return prefix + "/edit";
    }

    /**
     * 修改保存客户分享
     */
    @RequiresPermissions("system:userShare:edit")
    @Log(title = "客户分享", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(WebUserShare webUserShare)
    {
        return toAjax(webUserShareService.updateWebUserShare(webUserShare));
    }

    /**
     * 删除客户分享
     */
    @RequiresPermissions("system:userShare:remove")
    @Log(title = "客户分享", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(webUserShareService.deleteWebUserShareByIds(ids));
    }
}
